﻿using System.ComponentModel.Composition;
using System.Configuration;
using System.Windows.Forms;

using ExtensibleEventViewer.Core.Interfaces;
using ExtensibleEventViewer.Core.Primitives;


namespace ExtensibleEventViewer.EventLogFilters
{
	/// <summary>
	/// Represents filter which filters event log records based on their type
	/// </summary>
	[Export(typeof(IEventLogFilter))]
	public class EventLogRecordTypeFilter : ApplicationSettingsBase, IEventLogFilter
	{
		private const string SETTINGS_KEY = "Settings";

		private EventLogRecordTypeFilterControl _configurationControl;

		public bool IsMatch(EventLogRecord record)
		{
			switch (record.Type)
			{
				case EventLogRecordType.Error:
					return Settings.EnableErrorType;
				case EventLogRecordType.Information:
					return Settings.EnableInformationType;
				case EventLogRecordType.Warning:
					return Settings.EnableWarningType;
				case EventLogRecordType.Unknown:
					return Settings.EnableUnknownType;
				default:
					return true;
			}
		}

		public Control ConfigurationControl
		{
			get
			{
				if (_configurationControl == null || _configurationControl.IsDisposed)
				{
					_configurationControl = new EventLogRecordTypeFilterControl
					                        	{
					                        		Filter = this
					                        	};
				}

				return _configurationControl;
			}
		}

		public string FilterID
		{
			get
			{
				return "EventLogRecordTypeFilter";
			}
		}


		public override string ToString()
		{
			return FilterID;
		}

		[UserScopedSetting]
		public EventLogRecordTypeFilterSettings Settings
		{
			get
			{
				return this[SETTINGS_KEY] == null
						?
							new EventLogRecordTypeFilterSettings()
						:
							this[SETTINGS_KEY] as EventLogRecordTypeFilterSettings;
			}
			set
			{
				this[SETTINGS_KEY] = value;
			}
		}
	}
}
